/*
 * @Author: wangming
 * @Date: 2020-09-03 16:33:37
 * @LastEditors: wangming
 * @LastEditTime: 2021-12-14 11:16:20
 * @Description: file content
 */
import myMixin from '@/pageComponents/myMixin';
import NP from 'number-precision'
export default {
  mixins: [myMixin],
  methods: {
    format(num) {
      var reg = /\d{1,3}(?=(\d{3})+$)/g;
      return (num + '').replace(reg, '$&,');
    },
    Nformatter(num, limitNumber, flag = false) {
      num += '';
      var arr = num.split('.');
      var fixed = arr[1];
      var firstNum = flag ? this.format(arr[0]) : arr[0];
      if (fixed != undefined && limitNumber > 0) {
        let str = fixed.substring(0, limitNumber);
        return firstNum + '.' + str;
      } else {
        return firstNum;
      }
    },
  },
  render(h) {
    const { width, limitNumber, formula, showSeparator, currency = '' } = this.config.__config;
    let content = '0';
    if (formula.length > 0) {
      let str = formula.reduce((total, curr) => {
        let label = !!curr.key ? this.form[curr.key] || 0 : curr.label || 0;
        if (typeof label === 'object') {
          label = label.total;
        }
        return total + '' + label;
      }, '');
      try {
        content = this.Nformatter(NP.strip(eval(str)), limitNumber, false);
        this.$emit('input', content)
      } catch (error) {
        this.$emit('input', 0)
        this.$message.error(`${this.config.__config.label}  公式有误`);
      }
    }
    return h(
      'div',
      {
        attrs: {
          class: 'g9',
        },
        style: { width: width + '%', },
      },
      this.Nformatter(content, limitNumber, showSeparator) + ' ' + currency
    )
  },
};
